package ui.activity.card;

import java.sql.SQLException;

import ui.activity.exchange.CardQRCodeActivity;
import ui.model.card.CardDetailViewModel;
import util.web.SinaWeiboHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import business.entity.Card;
import business.service.card.ICardDetailViewService;
import business.service.card.impl.CardDetailViewService;

import com.lab107team.R;

public class CardDetailActivity extends Activity implements OnClickListener,
		android.content.DialogInterface.OnClickListener, Handler.Callback {

	private static final String TAG = CardDetailActivity.class.getSimpleName();

	private CardDetailViewModel model;
	private ICardDetailViewService service;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.card_detail_activity_layout);

		model = new CardDetailViewModel();
		service = new CardDetailViewService(model);

		initializeModel();
		initializeWidget();
		updateView();
	}

	private void initializeWidget() {
		((ImageButton) findViewById(R.id.btn_card_detail_back))
				.setOnClickListener(this);
		((Button) findViewById(R.id.btn_card_detail_share))
				.setOnClickListener(this);
		((ImageButton) findViewById(R.id.btn_card_detail_edit))
				.setOnClickListener(this);
		((LinearLayout) findViewById(R.id.ll_card_detail_telephone))
				.setOnClickListener(this);
		((LinearLayout) findViewById(R.id.ll_card_detail_email))
				.setOnClickListener(this);
	}

	private void initializeModel() {
		Intent intent = getIntent();
		int id = intent.getIntExtra("id", -1);
		Log.i(TAG, "当前正在查看ID为" + id + "的名片");
		try {
			model.setCard(service.search(id));
		} catch (SQLException e) {
			e.printStackTrace();
			Log.e(TAG, "无法找到ID为" + id + "的名片");
			finish();
		}
	}

	private void updateView() {
		Card card = model.getCard();
		((TextView) findViewById(R.id.tv_watch_card_info_context)).setText(card
				.getName());
		((TextView) findViewById(R.id.tv_watch_card_info_phone)).setText(card
				.getTelephone());
		((TextView) findViewById(R.id.tv_watch_card_info_email)).setText(card
				.getEmail());
		((TextView) findViewById(R.id.tv_watch_card_info_qq)).setText(card
				.getQq());
		((TextView) findViewById(R.id.tv_watch_card_info_weibo)).setText(card
				.getWeibo());

		if (card.getWeibo() != null && !card.getWeibo().equals("")) {
			SinaWeiboHelper.getWeibo(this, new Handler(this), card.getWeibo());
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_card_detail_back:
			onClickBack();
			break;

		case R.id.btn_card_detail_share:
			onClickShare();
			break;

		case R.id.btn_card_detail_edit:
			onClickEdit();
			break;

		case R.id.ll_card_detail_telephone:
			onTelephoneClick();
			break;

		case R.id.ll_card_detail_email:
			onEmailClick();
			break;

		default:
			break;
		}
	}

	private void onEmailClick() {
		try {
			Uri uri = Uri.parse("mailto:" + model.getCard().getEmail());
			Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
			startActivity(intent);
		} catch (Exception e) {
			Toast.makeText(CardDetailActivity.this, "无法打开系统默认的电子邮件APP",
					Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
	}

	private void onTelephoneClick() {
		if (model.getCard().getTelephone().equals("")) {
			return;
		}
		new AlertDialog.Builder(this).setTitle("提示").setMessage("选择操作方式")
				.setPositiveButton("打电话", this).setNegativeButton("发短信", this)
				.create().show();
	}

	private void onClickBack() {
		Log.i(TAG, "返回按钮被点击");
		finish();
	}

	private void onClickShare() {
		Log.i(TAG, "分享按钮被点击");
		Intent intent = new Intent(CardDetailActivity.this,
				CardQRCodeActivity.class);
		intent.putExtra("id", model.getCard().getId());
		startActivity(intent);
	}

	private void onClickEdit() {
		Log.i(TAG, "编辑按钮被点击");
		Intent intent = new Intent(CardDetailActivity.this,
				EditCardActivity.class);
		intent.putExtra("isAdd", false);
		intent.putExtra("id", model.getCard().getId());
		startActivity(intent);
		finish();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		Uri uri;
		Intent intent;

		switch (which) {
		case DialogInterface.BUTTON_POSITIVE:
			uri = Uri.parse("tel:" + model.getCard().getTelephone());
			intent = new Intent(Intent.ACTION_CALL, uri);
			startActivity(intent);
			break;

		case DialogInterface.BUTTON_NEGATIVE:
			uri = Uri.parse("smsto:" + model.getCard().getTelephone());
			intent = new Intent(Intent.ACTION_SENDTO, uri);
			startActivity(intent);

		default:
			break;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		Log.i(TAG, "Receive: " + msg.what + " " + (String) msg.obj);
		if (msg.what == SinaWeiboHelper.SUCCESS) {
			String weibo = model.getCard().getWeibo() + ": " + (String) msg.obj;
			((TextView) (findViewById(R.id.tv_watch_card_info_weibo)))
					.setText(weibo);
		}

		return false;
	}

}
